/**
 * 
 */
package systole.view.crud.patient.patientComponents.clinicalInfo;

import java.awt.Frame;
import java.text.DecimalFormat;
import javax.swing.JDialog;
import systole.domain.clinicalInformation.ClinicalInformation;
import systole.domain.clinicalInformation.standardValues.StandardParams;
import systole.domain.clinicalInformation.standardValues.StandardValue;

import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.view.crud.patient.patientComponents.controller.ControllerEditionPatienComponent;

/**
 * @author jmj
 *
 */
public class ControllerEditionClinicalInfoByPatient extends ControllerEditionPatienComponent {

    /**
     * @param patient
     */
    public ControllerEditionClinicalInfoByPatient(Patient patient) {
        super(patient);
        this.entityName = "Información clínica";
        this.newDescription = "Nueva";
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityFromForm()
     */
    @Override
    protected void loadEntityFromForm() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        ClinicalInformation clinicalInformation = (ClinicalInformation) this.curretnEntity;

        String value;

        clinicalInformation.setInformationDate(jDialogClinicalInfo.getjXDateAnalysis().getDate());

        if ((!jDialogClinicalInfo.getjTxtAge().isEditValid()) || (jDialogClinicalInfo.getjTxtAge().getText().isEmpty())) {
            value = "0";
        } else {
            value = jDialogClinicalInfo.getjTxtAge().getText();
        }
        clinicalInformation.setYears(Integer.parseInt(value));

        if ((!jDialogClinicalInfo.getjTxtHeight().isEditValid()) || (jDialogClinicalInfo.getjTxtHeight().getText().isEmpty())) {
            value = "0";
        } else {
            value = jDialogClinicalInfo.getjTxtHeight().getText();
        }
        clinicalInformation.setHeight(new Integer(value));

        if ((!jDialogClinicalInfo.getjTxtWeight().isEditValid()) || (jDialogClinicalInfo.getjTxtWeight().getText().isEmpty())) {
            value = "0";
        } else {
            value = jDialogClinicalInfo.getjTxtWeight().getText();
            value = value.replace(",", ".");
        }
        clinicalInformation.setWeight(new Float(value));

        if ((!jDialogClinicalInfo.getjTxtLDL().isEditValid()) || (jDialogClinicalInfo.getjTxtLDL().getText().isEmpty())) {
            value = "0";
        } else {
            value = jDialogClinicalInfo.getjTxtLDL().getText();
            value = value.replace(",", ".");
        }
        clinicalInformation.setLdl(new Float(value));

        if ((!jDialogClinicalInfo.getjTxtHDL().isEditValid()) || (jDialogClinicalInfo.getjTxtHDL().getText().isEmpty())) {
            value = "0";
        } else {
            value = jDialogClinicalInfo.getjTxtHDL().getText();
            value = value.replace(",", ".");
        }
        clinicalInformation.setHdl(new Float(value));

        if ((!jDialogClinicalInfo.getjTxtTotal().isEditValid()) || (jDialogClinicalInfo.getjTxtTotal().getText().isEmpty())) {
            value = "0";
        } else {
            value = jDialogClinicalInfo.getjTxtTotal().getText();
        }
        value = value.replace(",", ".");
        clinicalInformation.setTotalCholesterol(new Float(value));

        if ((!jDialogClinicalInfo.getjTxtTriglycerides().isEditValid()) || (jDialogClinicalInfo.getjTxtTriglycerides().getText().isEmpty())) {
            value = "0";
        } else {
            value = jDialogClinicalInfo.getjTxtTriglycerides().getText();
            value = value.replace(",", ".");
        }
        clinicalInformation.setTriglycerides(new Float(value));

        if ((!jDialogClinicalInfo.getjTxtPS().isEditValid()) || (jDialogClinicalInfo.getjTxtPS().getText().isEmpty())) {
            value = "0";
        } else {
            value = jDialogClinicalInfo.getjTxtPS().getText();
        }
        value = value.replace(",", ".");
        clinicalInformation.setSystolicPressure(new Float(value));


        if ((!jDialogClinicalInfo.getjTxtPD().isEditValid()) || (jDialogClinicalInfo.getjTxtPD().getText().isEmpty())) {
            value = "0";
        } else {
            value = jDialogClinicalInfo.getjTxtPD().getText();
        }
        value = value.replace(",", ".");
        clinicalInformation.setDiastolicPressure(new Float(value));

    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityOnForm()
     */
    @Override
    protected void loadEntityOnForm() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        ClinicalInformation clinicalInformation = (ClinicalInformation) this.curretnEntity;

        jDialogClinicalInfo.getjXDateAnalysis().setDate(clinicalInformation.getInformationDate());

        DecimalFormat decimalFormat = new DecimalFormat("##0.00");

        if (clinicalInformation.getYears() != null) {
            jDialogClinicalInfo.getjTxtAge().setText(clinicalInformation.getYears().toString());
        }

        if (clinicalInformation.getGlucemic() != null) {
            jDialogClinicalInfo.getjTxtGlucemy().setText(decimalFormat.format(clinicalInformation.getGlucemic()));
        }

        if (clinicalInformation.getHeight() != null) {
            jDialogClinicalInfo.getjTxtHeight().setText(clinicalInformation.getHeight().toString());
        }

        if (clinicalInformation.getWeight() != null) {
            jDialogClinicalInfo.getjTxtWeight().setText(decimalFormat.format(clinicalInformation.getWeight()));
        }

        if (clinicalInformation.getHdl() != null) {
            jDialogClinicalInfo.getjTxtHDL().setText(decimalFormat.format(clinicalInformation.getHdl()));
        }

        if (clinicalInformation.getLdl() != null) {
            jDialogClinicalInfo.getjTxtLDL().setText(decimalFormat.format(clinicalInformation.getLdl()));
        }

        if (clinicalInformation.getTotalCholesterol() != null) {
            jDialogClinicalInfo.getjTxtTotal().setText(decimalFormat.format(clinicalInformation.getTotalCholesterol()));
        }

        if (clinicalInformation.getTriglycerides() != null) {
            jDialogClinicalInfo.getjTxtTriglycerides().setText(decimalFormat.format(clinicalInformation.getTriglycerides()));
        }

        if (clinicalInformation.getSystolicPressure() != null) {
            jDialogClinicalInfo.getjTxtPS().setText(decimalFormat.format(clinicalInformation.getSystolicPressure()));
        }

        if (clinicalInformation.getDiastolicPressure() != null) {
            jDialogClinicalInfo.getjTxtPD().setText(decimalFormat.format(clinicalInformation.getDiastolicPressure()));
        }
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#save()
     */
    @Override
    protected void save() {
        if (!this.isEditing()) {
            this.patient.getClinicalInformation().add((ClinicalInformation) this.curretnEntity);
        }
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#valid()
     */
    @Override
    protected String valid() {

        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        Float valueF;
        String valueS;
        Integer valueI;

        if (jDialogClinicalInfo.getjXDateAnalysis().getDate() == null) {
            return "Debe ingresar la fecha del análisis";
        }

        if ((jDialogClinicalInfo.getjTxtAge().getText() == null) || (jDialogClinicalInfo.getjTxtAge().getText().isEmpty())) {
            return "Debe ingresar la edad";
        }


        if ((jDialogClinicalInfo.getjTxtWeight().getText() == null) || (jDialogClinicalInfo.getjTxtWeight().getText().isEmpty())) {
            return "Debe ingresar el peso";
        }

        valueS = jDialogClinicalInfo.getjTxtWeight().getText();
        valueS = valueS.replace(",", ".");
        try {
            valueF = new Float(valueS);
            if (valueF.floatValue() <= 0) {
                return "Debe ingresar un peso válido";
            }
        } catch (NumberFormatException e) {
            return "Debe ingresar un peso válido";
        }

        if ((jDialogClinicalInfo.getjTxtHeight().getText() == null) || (jDialogClinicalInfo.getjTxtHeight().getText().isEmpty())) {
            return "Debe ingresar la altura";
        }

        valueS = jDialogClinicalInfo.getjTxtHeight().getText();
        try {
            valueI = new Integer(valueS);
            if (valueI.intValue() <= 0) {
                return "Debe ingresar una altura válida";
            }
        } catch (NumberFormatException e) {
            return "Debe ingresar una altura válida";
        }
        return null;
    }

    @Override
    protected void loadIconOnForm() {
    }

    @Override
    protected Object createEntity() throws ExceptionDAO {
        return new ClinicalInformation(this.patient);
    }

    @Override
    protected JDialog createEditionForm(Frame parent) {
        return new JDialogClinicalInfo(parent, this);
    }



    public void setNormalValueToLDL() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st =this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.LDL);
            jDialogClinicalInfo.getjTxtLDL().setText(st != null ? st.getNormal().toString() : StandardParams.LDL.getNormal().toString());
        } catch (ExceptionDAO e) {
            jDialogClinicalInfo.getjTxtLDL().setText(StandardParams.LDL.getNormal().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setMediumValueToLDL() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.LDL);
           jDialogClinicalInfo.getjTxtLDL().setText(st != null ? st.getMedium().toString() : StandardParams.LDL.getMedium().toString());
        } catch (ExceptionDAO edao) {
            jDialogClinicalInfo.getjTxtLDL().setText(StandardParams.LDL.getMedium().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setHighValueToLDL() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.LDL);
            jDialogClinicalInfo.getjTxtLDL().setText(st != null ? st.getHigh().toString() : StandardParams.LDL.getHigh().toString());
        } catch (ExceptionDAO edao) {
            jDialogClinicalInfo.getjTxtLDL().setText(StandardParams.LDL.getHigh().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setNormalValueToHDL() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.HDL);
            jDialogClinicalInfo.getjTxtHDL().setText(st != null ? st.getNormal().toString() : StandardParams.HDL.getNormal().toString());
        } catch (ExceptionDAO e) {
            jDialogClinicalInfo.getjTxtHDL().setText(StandardParams.HDL.getNormal().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setMediumValueToHDL() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.HDL);
            jDialogClinicalInfo.getjTxtHDL().setText(st != null ? st.getMedium().toString() : StandardParams.HDL.getMedium().toString());
        } catch (ExceptionDAO edao) {
           jDialogClinicalInfo.getjTxtHDL().setText(StandardParams.HDL.getMedium().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setHighValueToHDL() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.HDL);
           jDialogClinicalInfo.getjTxtHDL().setText(st != null ? st.getHigh().toString() : StandardParams.HDL.getHigh().toString());
        } catch (ExceptionDAO edao) {
            jDialogClinicalInfo.getjTxtHDL().setText(StandardParams.HDL.getHigh().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setNormalValueToCTotal() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.TOTAL);
            jDialogClinicalInfo.getjTxtTotal().setText(st != null ? st.getNormal().toString() : StandardParams.TOTAL.getNormal().toString());
        } catch (ExceptionDAO e) {
            jDialogClinicalInfo.getjTxtTotal().setText(StandardParams.TOTAL.getNormal().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setMediumValueToCTotal() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.TOTAL);
           jDialogClinicalInfo.getjTxtTotal().setText(st != null ? st.getMedium().toString() : StandardParams.TOTAL.getMedium().toString());
        } catch (ExceptionDAO edao) {
            jDialogClinicalInfo.getjTxtTotal().setText(StandardParams.TOTAL.getMedium().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setHighValueToCTotal() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.TOTAL);
            jDialogClinicalInfo.getjTxtTotal().setText(st != null ? st.getHigh().toString() : StandardParams.TOTAL.getHigh().toString());
        } catch (ExceptionDAO edao) {
            jDialogClinicalInfo.getjTxtTotal().setText(StandardParams.TOTAL.getHigh().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setNormalValueToTriglycerides() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.TRIGLYCERIDES);
            jDialogClinicalInfo.getjTxtTriglycerides().setText(st != null ? st.getNormal().toString() : StandardParams.TRIGLYCERIDES.getNormal().toString());
        } catch (ExceptionDAO e) {
            jDialogClinicalInfo.getjTxtTriglycerides().setText(StandardParams.TRIGLYCERIDES.getNormal().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setMediumValueToTriglycerides() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.TRIGLYCERIDES);
            jDialogClinicalInfo.getjTxtTriglycerides().setText(st != null ? st.getMedium().toString() : StandardParams.TRIGLYCERIDES.getMedium().toString());
        } catch (ExceptionDAO edao) {
            jDialogClinicalInfo.getjTxtTriglycerides().setText(StandardParams.TRIGLYCERIDES.getMedium().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setHighValueToTriglycerides() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.TRIGLYCERIDES);
            jDialogClinicalInfo.getjTxtTriglycerides().setText(st != null ? st.getHigh().toString() : StandardParams.TRIGLYCERIDES.getHigh().toString());
        } catch (ExceptionDAO edao) {
            jDialogClinicalInfo.getjTxtTriglycerides().setText(StandardParams.TRIGLYCERIDES.getHigh().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setNormalValueToGlucemy() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.GLUCEMY);
            jDialogClinicalInfo.getjTxtGlucemy().setText(st != null ? st.getNormal().toString() : StandardParams.GLUCEMY.getNormal().toString());
        } catch (ExceptionDAO e) {
            jDialogClinicalInfo.getjTxtGlucemy().setText(StandardParams.GLUCEMY.getNormal().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setMediumValueToGlucemy() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.GLUCEMY);
            jDialogClinicalInfo.getjTxtGlucemy().setText(st != null ? st.getMedium().toString() : StandardParams.GLUCEMY.getMedium().toString());
        } catch (ExceptionDAO edao) {
            jDialogClinicalInfo.getjTxtGlucemy().setText(StandardParams.GLUCEMY.getMedium().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setHighValueToGlucemy() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.GLUCEMY);
            jDialogClinicalInfo.getjTxtGlucemy().setText(st != null ? st.getHigh().toString() : StandardParams.GLUCEMY.getHigh().toString());
        } catch (ExceptionDAO edao) {
            jDialogClinicalInfo.getjTxtGlucemy().setText(StandardParams.GLUCEMY.getHigh().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setNormalValueToPS() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.PS);
           jDialogClinicalInfo.getjTxtPS().setText(st != null ? st.getNormal().toString() : StandardParams.PS.getNormal().toString());
        } catch (ExceptionDAO e) {
            jDialogClinicalInfo.getjTxtPS().setText(StandardParams.PS.getNormal().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setMediumValueToPS() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.PS);
            jDialogClinicalInfo.getjTxtPS().setText(st != null ? st.getMedium().toString() : StandardParams.PS.getMedium().toString());
        } catch (ExceptionDAO edao) {
            jDialogClinicalInfo.getjTxtPS().setText(StandardParams.PS.getMedium().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setHighValueToPS() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.PS);
            jDialogClinicalInfo.getjTxtPS().setText(st != null ? st.getHigh().toString() : StandardParams.PS.getHigh().toString());
        } catch (ExceptionDAO edao) {
            jDialogClinicalInfo.getjTxtPS().setText(StandardParams.PS.getHigh().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setNormalValueToPD() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.PD);
            jDialogClinicalInfo.getjTxtPD().setText(st != null ? st.getNormal().toString() : StandardParams.PD.getNormal().toString());
        } catch (ExceptionDAO e) {
            jDialogClinicalInfo.getjTxtPD().setText(StandardParams.PD.getNormal().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setMediumValueToPD() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.PD);
            jDialogClinicalInfo.getjTxtPD().setText(st != null ? st.getMedium().toString() : StandardParams.PD.getMedium().toString());
        } catch (ExceptionDAO edao) {
            jDialogClinicalInfo.getjTxtPD().setText(StandardParams.PD.getMedium().toString());
            this.facadeDB.refreshSession();
        }
    }


    public void setHighValueToPD() {
        JDialogClinicalInfo jDialogClinicalInfo = (JDialogClinicalInfo) this.editForm;
        try {
            StandardValue st = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.PD);
            jDialogClinicalInfo.getjTxtPD().setText(st != null ? st.getHigh().toString() : StandardParams.PD.getHigh().toString());
        } catch (ExceptionDAO edao) {
            jDialogClinicalInfo.getjTxtPD().setText(StandardParams.PD.getHigh().toString());
            this.facadeDB.refreshSession();
        }
    }
}
